############################################### Lib options
MYSQL_CFLAGS = $(shell mysql_config --cflags )
MYSQL_LIBS = $(shell mysql_config --libs)

############################################### Compile options
CC = g++
CFLAGS = -std=c++11 -Winvalid-offsetof -Wno-invalid-offsetof


############################################### Public basic header files 
# All
ALL_INCS = -I src/db \
	-I src/db/mysql \
	-I src/log \
	-I src/main \
	-I src/core \
	-I src/include \
	-I objs

# Include
INCLUDE_DEPS = src/include/db.h
INCLUDE_INCS = -I src/include

# Core
CORE_INCS = -I src/core

# Db 
DB_DEPS = src/db/db_core.h \
	src/db/db_compiler.h \
	src/db/mysql/db_driver.h \
	src/db/db_conn_pool.h \
	src/db/db_connection.h \
	src/db/db_result.h \
	src/db/db_tables.h \
	src/db/db_field.h \
	src/db/db_model.h \
	src/db/db_manager.h \
	src/db/db_queryset.h \
	src/db/db_query.h \
	src/db/db_util.h
DB_INCS = -I src/db \
	-I src/db/mysql

# Log
LOG_DEPS = src/log/log.h \
	src/log/log_base.h
LOG_INCS = -I src/log

# Main
MAIN_DEPS = src/main/db_main.h
MAIN_INCS = -I src/main


############################################### link object files
TARGET = objs/KernelSec
OBJ = src/main/main.o \
	  src/log/log.o \
	  src/log/log_base.o \
	  src/db/db_compiler.o \
	  src/db/mysql/db_driver.o \
	  src/db/db_conn_pool.o \
	  src/db/db_connection.o \
	  src/db/db_field.o \
	  src/db/db_util.o \
	  src/db/db_query.o \
	  src/db/db_queryset.o \
	  src/db/db_manager.o \
	  src/db/db_model.o \
	  src/db/db_tables.o \
	  src/db/db_result.o 
	  

############### Link  # exec file name `date "+%Y-%m-%d"`
KernelSec:$(OBJ)
	$(CC) $(DEBUG) $(CFLAGS) -o $(TARGET) $(OBJ) $(MYSQL_LIBS) -lpthread -lcrypt -lcrypto -lcrypto -lz

############### Main part
src/main/main.o: src/main/db_main.cpp
	$(CC) $(DEBUG) $(CFLAGS) -c $(MAIN_INCS) src/main/db_main.cpp -o src/main/main.o

############### Log part
src/log/log.o: src/log/log.cpp $(LOG_DEPS) $(CORE_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c $(LOG_INCS) src/log/log.cpp -o src/log/log.o

src/log/log_base.o: src/log/log_base.cpp $(LOG_DEPS) $(CORE_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/log/log_base.cpp -o src/log/log_base.o

############### DB part
src/db/db_compiler.o: src/db/db_compiler.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_compiler.cpp -o src/db/db_compiler.o

src/db/mysql/db_driver.o: src/db/mysql/db_driver.cpp $(DB_DEPS) 
	$(CC) $(DEBUG) $(CFLAGS) -c $(MYSQL_CFLAGS) src/db/mysql/db_driver.cpp $(MYSQL_LIBS) -o src/db/mysql/db_driver.o

src/db/db_conn_pool.o: src/db/db_conn_pool.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_conn_pool.cpp -o src/db/db_conn_pool.o

src/db/db_connection.o: src/db/db_connection.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_connection.cpp -o src/db/db_connection.o

src/db/db_field.o: src/db/db_field.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_field.cpp -o src/db/db_field.o

src/db/db_util.o: src/db/db_util.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_util.cpp -o src/db/db_util.o

src/db/db_query.o: src/db/db_query.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_query.cpp -o src/db/db_query.o

src/db/db_queryset.o: src/db/db_queryset.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_queryset.cpp -o src/db/db_queryset.o

src/db/db_manager.o: src/db/db_manager.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_manager.cpp -o src/db/db_manager.o

src/db/db_model.o: src/db/db_model.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_model.cpp -o src/db/db_model.o

src/db/db_tables.o: src/db/db_tables.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_tables.cpp -o src/db/db_tables.o

src/db/db_result.o: src/db/db_result.cpp $(DB_DEPS)
	$(CC) $(DEBUG) $(CFLAGS) -c src/db/db_result.cpp -o src/db/db_result.o

	
.PHONY:clean
clean:
	rm -f *.o
	rm -f src/*.o
	rm -f src/main/*.o
	rm -f src/log/*.o
	rm -f src/db/*.o
	rm -f src/db/mysql/*.o
	rm -f src/core/*.o
	rm -f src/include/*.o


